package com.xinqi.modules.chat.chat.message.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.chat.chat.message.domain.ChatMessageCountEntity;
import com.xinqi.modules.chat.chat.message.manager.ChatMessageCountManager;
import com.xinqi.modules.chat.chat.message.mapper.ChatMessageCountMapper;
import org.springframework.stereotype.Service;

import lombok.RequiredArgsConstructor;

import java.util.Objects;

/**
 * 陌生人聊天限制管理层实现
 *
 * @author: yyzhen
 * @date: 2023/06/02
 */
@Service
@RequiredArgsConstructor
public class ChatMessageCountManagerImpl extends BaseServiceImpl<ChatMessageCountMapper, ChatMessageCountEntity> implements ChatMessageCountManager {

    /**
     * 获取陌生人聊天限制数据 -- 没有则初始化
     *
     * @param receiveId
     * @param sendId
     * @return
     */
    @Override
    public ChatMessageCountEntity initCount(Long receiveId, Long sendId) {
        LambdaQueryWrapper<ChatMessageCountEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatMessageCountEntity::getSendId, sendId)
            .eq(ChatMessageCountEntity::getReceiveId, receiveId)
            .last("LIMIT 1");
        ChatMessageCountEntity entity = this.getOne(queryWrapper);
        if (Objects.isNull(entity)) {
            entity = new ChatMessageCountEntity();
            entity.setReceiveId(receiveId);
            entity.setSendId(sendId);
            entity.setUsedCount(0);
            this.save(entity);
        }
        return entity;
    }

    /**
     * 根据用户ID及陌生人ID删除陌生人聊天限制
     *
     * @param receiveId
     * @param sendId
     * @return
     */
    @Override
    public Boolean deleteById(Long receiveId, Long sendId) {
        LambdaQueryWrapper<ChatMessageCountEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatMessageCountEntity::getSendId, sendId)
            .eq(ChatMessageCountEntity::getReceiveId, receiveId);
        return this.remove(queryWrapper);
    }
}
